/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.web.generator;

import com.farmcode.api.runtime.FarmcodeContext;
import com.farmcode.base.web.BaseBacking;
import com.farmcode.console.api.model.Project;
import com.farmcode.console.api.model.Property;
import java.util.List;

/**
 *
 * @author Oscar Ivan Prado
 */
public class BaseCodeGenerator extends BaseBacking {

    public static final String FARMCODE_CONTEXT = "com.farmcode.web.generator.FARMCODE_CONTEXT";
    private boolean enabled;
    private FarmcodeContext context;
    private Project project;

    protected void newContext() {
        setContext(new FarmcodeContext());
    }

    protected void newContext(List<Property> parameters, List<Property> variables) {
        newContext();
        
        if (getProject() != null) {
            
            context.put(FarmcodeContext.PROJECT_NAME, project.getName());
            context.put(FarmcodeContext.TEMPLATE_PATH, project.getPackageTemplate().getName());
            getSession().setValue(project.getPackageTemplate().getClass().getCanonicalName(), project.getPackageTemplate());
            
            for (Property property : parameters) {
                context.put(property.getName(), property.getValue());
            }
            
            for (Property property : variables) {
                context.put(property.getName(), property.getValue());
            }

            if (project.getLanguage() != null) {
                context.put(FarmcodeContext.LANGUAGE, project.getLanguage().getName());
            }

            if (project.getDatabase() != null) {
                context.put(FarmcodeContext.DATABASE, project.getDatabase().getName());
            }
            
            setContext(context);
        }

    }
    

    protected FarmcodeContext getContext() {
        if (getSession().getValue(FARMCODE_CONTEXT) != null) {
            context = getSession().getValue(FARMCODE_CONTEXT);
        } else {
            newContext();
        }
        return context;
    }

    protected void setContext(FarmcodeContext context) {
        this.context = context;
        getSession().setValue(FARMCODE_CONTEXT, context);
    }

    /**
     * @return the enabled
     */
    public boolean isEnabled() {
        enabled = getContext().size() > 0;
        return enabled;
    }

    /**
     * @param enabled the enabled to set
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    /**
     * @return the project
     */
    protected Project getProject() {
        project = new Project();
        if(getSession().getValue(project.getClass().getCanonicalName()) != null){
            project = getSession().getValue(project.getClass().getCanonicalName());
        }
        return project;
    }

    /**
     * @param project the project to set
     */
    protected void setProject(Project project) {
        this.project = project;
        getSession().setValue(project.getClass().getCanonicalName(), project);
    }
}
